package com.example.wechat.util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.wechat.R;

public class ImageLoader {
    private static final RequestOptions DEFAULT_OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.drawable.default_image)
            .error(R.drawable.default_image);

    public static void loadImage(Context context, String url, ImageView imageView) {
        loadImage(context, url, imageView, null);
    }

    public static void loadImage(Context context, String url, ImageView imageView, RequestOptions options) {
        if (options == null) {
            options = DEFAULT_OPTIONS;
        }

        Glide.with(context)
                .load(url)
                .apply(options)
                .into(imageView);
    }

    public static void loadImageWithProgress(Context context, String url, ImageView imageView, ProgressTarget.ProgressListener listener) {
        Glide.with(context)
                .load(url)
                .apply(DEFAULT_OPTIONS)
                .into(new ProgressTarget<Drawable>(imageView) {
                    @Override
                    public void onProgressUpdate(int progress) {
                        if (listener != null) {
                            listener.onProgress(progress);
                        }
                    }
                });
    }

    public static void loadCircleImage(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .apply(DEFAULT_OPTIONS.circleCrop())
                .into(imageView);
    }

    public static void loadRoundCornerImage(Context context, String url, ImageView imageView, int radius) {
        Glide.with(context)
                .load(url)
                .apply(DEFAULT_OPTIONS.transform(new RoundedCornersTransformation(radius)))
                .into(imageView);
    }
} 